package cn.lu.joke.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.lu.joke.util.FragmentManager;
import cn.lu.joke.util.LogUtils;

public abstract class BaseFragment extends Fragment {

    private Unbinder mUnbinder;
    protected String mTitle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View layout  = inflater.inflate(getLayout(), null);
        mUnbinder = ButterKnife.bind(this, layout);
        return layout;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentManager.get().addFragment(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (isAdded() && !hidden && getUserVisibleHint() && getView() != null) {
            onVisible();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isAdded() && !isHidden() && getUserVisibleHint() && getView() != null) {
            onVisible();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        FragmentManager.get().removeFragment(this);
    }

    public String getTitle() {
        return mTitle;
    }

    protected abstract int getLayout();

    protected abstract void initView();

    public void initData(){

    }

    public void onVisible() {

    }

    public void showLoading() {

    }

    public void hiddenLoading() {

    }

    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("onDetach");
    }
}
